是一種程式設計的模式,主要理念是為了減少重複出現的邏輯,比方說 log 紀錄,登入驗證、資料驗證等重複性事務,如果多個方法都需要執行到這類方法,就可以抽出來透過定義在切面中,減少程式耦合及方便去進行維護,讓開發者可以專注在更重要的業務邏輯中。
如同下圖每個箭頭是不同的方法或流程,如每個方法都要加入同樣的處理,就可以當成一個切面抽出來所以像是 log, security 的部分就被稱為 cross-cutting concerns 但都可以抽出共同需要處理的部分作為切面(藍色及綠色)。

@Aspect,且注意需要加上@Component 將類別標記成為 bean 才可以使用@Component
@Aspect
public class LogAspect {
    // 撰寫切面處理方法及控制發動時間..
}
@Pointcut 標記為切入點套用切面@Component
@Aspect
public class LogAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    // com.example.demoaop.TestController class 內所有的方法(不包含子 package)會生效
    @Pointcut("execution(* com.example.demoaop.TestController.*(..))")
    public void pointcut() {
    }
}
關於切入點 execution 表達式:
切入點為 com.example.demo.Test 底下的 print() 方法
execution(* com.example.demo.Test.print())
切入點為 com.example.demo.Test 底下的所有方法
execution(* com.example.demo.Test.*(..))
切入點為 com.example.demo 這個 package 中的所有 class 的所有方法(不包含子 package)
execution(* com.example.demo.*(..))
切入點為 com.example.demo 這個 package 及其底下所有子 package 中的所有 class 的所有方法
execution(* com.example.demo..*(..))
另外控制切面的執行被稱為 Advice ,Spring 有提供 5 種 Advice 可以讓你操作方法需要執行的時間
@Before : 在切入點執行前前執行切面。@After : 在切入點執行後後執行切面。@AfterReturning : 在切入點傳回(return)內容後執行,可以對傳回內容進行一些加工處理。@Around : 在切入點前後執行切面,並配合 proceed 可控制何時執行切入點本身的內容。@AfterThrowing : 當切入點拋出例外後執行package com.example.demoaop;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class TestController {
    @GetMapping("/test/{word}")
    public String test(@PathVariable String word) throws IOException {
        if (word.equals("yes")) {
            throw new RuntimeException();
        }
        return word;
    }
}
package com.example.demoaop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
@Component
@Aspect
public class LogAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Pointcut("execution(* com.example.demoaop.TestController.*(..))")
    public void pointcut() {
    }
    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("*****before advice start*****");
        logger.info("do before " + joinPoint.getSignature().getName());
        Arrays.stream(joinPoint.getArgs()).forEach(System.out::println);
        System.out.println("*****before advice end*****");
    }
    @After("pointcut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("-----after advice start-----");
        logger.info("do after " + joinPoint.getSignature().getName());
        System.out.println("-----after advice end-----");
    }
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("=====Around advice starts=====");
        long startTime = System.currentTimeMillis();
        // 呼叫proceed() 方法開始執行原方法
        Object result = joinPoint.proceed();
        long spentTime = System.currentTimeMillis() - startTime;
        logger.info(joinPoint.getSignature().getName() + " Time spent: " + spentTime);
        System.out.println("=====Around advice ends=====");
        return result;
    }
    @AfterReturning(pointcut = "pointcut()", returning = "result")
    public void logReturnResponse(Object result) { // 紀錄 Response
        System.out.println("=====after returning advice starts=====");
        if (result != null) {
            System.out.println(result);
        }
        System.out.println("=====after returning advice ends=====");
    }
    @AfterThrowing(pointcut = "pointcut()", throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint, Throwable throwable) {
        System.out.println("=====after throwing advice starts=====");
        System.out.println(joinPoint.getSignature().getName());
        System.out.println(throwable);
        System.out.println("=====after throwing advice ends=====");
    }
}
如果當我啟動後並且透過 TestController 的 test 方法進行請求並帶入 "no",就會看到以下 log 顯示

around start ->
before start -> before method -> before end ->
after returning start -> after returning method -> after returning end ->
after start -> after method -> after end ->
(proceed 控制執行 around 內容) around method -> around end
如果是請求帶 yes 導致中途拋出異常就會變成

around start ->
before start -> before method -> before end ->
(拋出錯誤所以沒有 return 不執行 after returning,改執行 after throwing) after throwing start -> after throwing method -> after throwing end ->
after start -> after method -> after end -> 拋錯
*after 不管有沒有拋錯都執行
所以這就是基本 AOP 的用法,大家應該對於相關的切面控制都有一定的了解了,不妨可以嚐試加入自己的專案內,針對一些特定位置重複會進行的方法或是 log 紀錄抽出來作為切面,這樣設計可以讓程式碼更加簡潔。
Ref:
相關文章也會同步更新我的部落格,有興趣也可以在裡面找其他的技術分享跟資訊。